home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / comm / tcp / AmigaTCP.lha / AmigaTCP / src / tnserv.c < prev    next >
C/C++ Source or Header  |  1989-06-24  |  2KB  |  104 lines

  1. #include <stdio.h>
  2. #include "machdep.h"
  3. #include "mbuf.h"
  4. #include "timer.h"
  5. #include "internet.h"
  6. #include "icmp.h"
  7. #include "netuser.h"
  8. #include "tcp.h"
  9. #include "telnet.h"
  10. #include "session.h"
  11.  
  12. struct tcb *tnet_tcb;
  13. telnet_start(argc,argv)
  14. char *argv[];
  15. {
  16.     struct socket lsocket;
  17.     extern int32 ip_addr;
  18.     void tnet_state();
  19.     void t_state(),rcv_char();
  20.  
  21.     /* Incoming Telnet */
  22.     lsocket.address = ip_addr;
  23.     if(argc < 2)
  24.         lsocket.port = TELNET_PORT;
  25.     else
  26.         lsocket.port = atoi(argv[1]);
  27.     tnet_tcb = open_tcp(&lsocket,NULLSOCK,TCP_PASSIVE,0,rcv_char,NULLVFP,tnet_state,0,(int *)NULL);
  28. }
  29. /* Handle incoming Telnet connect requests by creating a Telnet session,
  30.  * then change upcall vector so it behaves like an ordinary Telnet session.
  31.  * 
  32.  */
  33. static void
  34. tnet_state(tcb,old,new)
  35. struct tcb *tcb;
  36. char old,new;
  37. {
  38.     struct telnet *tn;
  39.     struct session *s,*newsession();
  40.     void t_state();
  41.     char *calloc();
  42.  
  43.     switch(new){
  44.     case ESTABLISHED:
  45.         log(tcb,"open Telnet");
  46.         /* Allocate a session descriptor */
  47.         if((s = newsession()) == NULLSESSION){
  48.             printf("\007Incoming Telnet call from %s refused; too many sessions\r\n",
  49.              psocket(&tcb->conn.remote));
  50.             fflush(stdout);
  51.             sndmsg(tcb,"Call rejected; too many sessions on remote system\r\n");
  52.             close_tcp(tcb);
  53.             return;
  54.         }
  55.         s->type = TELNET;
  56.         s->parse = send_tel;
  57.         /* Create and initialize a Telnet protocol descriptor */
  58.         if((tn = (struct telnet *)calloc(1,sizeof(struct telnet))) == NULLTN){
  59.             printf("\007Incoming Telnet call refused; no space\r\n");
  60.             fflush(stdout);
  61.             sndmsg(tcb,"Call rejected; no space on remote system\r\n");
  62.             close_tcp(tcb);
  63.             s->type = FREE;
  64.             return;
  65.         }
  66.         tn->session = s;    /* Upward pointer */
  67.         tn->state = TS_DATA;
  68.         s->cb.telnet = tn;    /* Downward pointer */
  69.  
  70.         tcb->user = (int *)tn;    /* Upward pointer */
  71.         tn->tcb = tcb;        /* Downward pointer */
  72.         printf("\007Incoming Telnet session %u from %s\r\n",s - sessions,
  73.             psocket(&tcb->conn.remote));
  74.         fflush(stdout);
  75.         tcb->s_upcall = t_state;
  76.         return;
  77.     case CLOSED:
  78.         /* This will only happen if the connection closed before
  79.          * the session was set up, e.g., if we refused it because
  80.          * there were too many sessions, or if the server is being
  81.          * shut down.
  82.          */
  83.         if(tcb == tnet_tcb)
  84.             tnet_tcb = NULLTCB;
  85.         del_tcp(tcb);
  86.         break;
  87.     }
  88. }
  89. telnet_stop()
  90. {
  91.     if(tnet_tcb != NULLTCB)
  92.         close_tcp(tnet_tcb);
  93. }
  94. static
  95. sndmsg(tcb,msg)
  96. struct tcb *tcb;
  97. char *msg;
  98. {
  99.     struct mbuf *bp;
  100.  
  101.     bp = qdata(msg,(int16)strlen(msg));
  102.     send_tcp(tcb,bp);
  103. }
  104.